﻿using System.Collections.Generic;
using System.Web.Mvc;
using BLL.Installer;
using BLL.Interface.OperatingSystem;
using BLL.Model.OperatingSystem;
using BLL.Utility;
using WayCloud.ViewModel;

namespace WayCloud.Controllers.OperatingSystem
{
    [Authorize(Roles = "admin")]
    public class OperatingSystemController : Controller
    {
        public IOperatingSystemInfo OperatingSystemInfoManager { get; set; }

        public OperatingSystemController()
        {
            OperatingSystemInfoManager = ContainerHelper.Instance.Container.Resolve<IOperatingSystemInfo>();
        }

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult List()
        {
            AnalyseJqGridQueryString ajqs = new AnalyseJqGridQueryString(Request.QueryString);
            var page = ajqs.GetPageInfo();
            var sort = ajqs.GetSortField();
            var condition = new Dictionary<string, string>();
            if (!string.IsNullOrWhiteSpace(Request.QueryString["q"]))
            {
                condition.Add("Name", Request.QueryString["q"]);
            }
            var result = OperatingSystemInfoManager.GetOperatingSystemInfoByFilter(condition, sort, page);
            var jqResult = new JqGrid(page, result);
            return Json(jqResult, JsonRequestBehavior.AllowGet);
        }

        public ActionResult FindOperatingSystemInfoById(string id)
        {
            if (string.IsNullOrWhiteSpace(id))
            {
                return View("Add");
            }
            var result = OperatingSystemInfoManager.FindOperatingSystemInfoById(int.Parse(id));
            return View("Edit", result);
        }

        public ActionResult Edit(OperatingSystemInfo osInfo)
        {
            if (!ModelState.IsValid)
            {
                return View("Edit", osInfo);
            }
            var result = OperatingSystemInfoManager.UpdateOperatingSystemInfo(osInfo);
            return result ? Content("ok") : (ActionResult)View("Edit", osInfo);
        }

        public ActionResult Delete(string id)
        {
            OperatingSystemInfoManager.DeleteOperatingSystemInfoById(int.Parse(id));
            return Content("ok");
        }

        public ActionResult Add(OperatingSystemInfo osInfo)
        {
            if (!ModelState.IsValid)
            {
                return View(osInfo);
            }
            var result = OperatingSystemInfoManager.AddOperatingSystemInfo(osInfo);
            return result ? Content("ok") : (ActionResult)View(osInfo);
        }

    }
}
